<?php
require_once 'storage/AMStorage.php';
require_once 'storage/CMdb.php';
require_once 'storage/MStorageException.php';

class MSqliteStorage extends AMStorage {
	
	private function __construct(){$this->dbh = null;}
	
	public static function instance($path = null){
		
		if(null === self::$dbh){
			
			if(null === $path):
			//$this->dbh = sqlite_open(CMdb::MAIN_SQLITE_PATH);
			self::$dbh = new SQLiteDatabase(CMdb::MAIN_SQLITE_PATH);
			elseif(!is_file($path)):
			throw new MStorareException('FATAL_ERROR:'.__FILE__.' L.'.__LINE__.' ['.$path.'] is NOT a valid file.');
			elseif(!is_writable($path)):
			throw new MStorareException('FATAL_ERROR:'.__FILE__.' L.'.__LINE__.' ['.$path.'] is NOT a writable file, please check your file system permission.');
			else:
			//$this->dbh = sqlite_open($path);
			self::$dbh = new SQLiteDatabase(CMdb::MAIN_SQLITE_PATH);
			endif;
		
			return self::$dbh;
			
		} else {
			
			return self::$dbh;
			
		}
	}
	
	public function exec(){
		if(!$this->query) return false;
		
		if(null === $this->dbh)  $this->connect();
		
		return sqlite_query($this->query, $this->dbh);
	}
	public function add(){}
	public function update(){}
	public function remove(){}
	public function close(){
		sqlite_close($this->dbh);
	}
}
?>